home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2410 / 2410.xpi / chrome / content / foxmarks-update.js < prev    next >
Text File  |  2010-01-28  |  4KB  |  130 lines

  1. /*
  2.  Copyright 2005-2007 Foxmarks Inc.
  3.  
  4.  foxmarks-update.js: implements interface to Firefox extensions manager for
  5.  handling updates of the extension.
  6.  
  7.  */
  8.  
  9.  
  10. var em;
  11. var Ci = Components.interfaces;
  12. var Cc = Components.classes;
  13.  
  14. function ForceUpdate() {
  15.     // find a window to use as a parent for open
  16.     var topwin = Cc['@mozilla.org/appshell/window-mediator;1'].
  17.         getService(Ci.nsIWindowMediator).
  18.         getMostRecentWindow(null);
  19.  
  20.     // first, open the extension manager window
  21.     em = topwin.
  22.      open("chrome://mozapps/content/extensions/extensions.xul?type=extensions",
  23.         "_blank", "chrome,dialog=no,resizable");
  24.     em.addEventListener("pageshow", ExtensionManagerShow, false);
  25.  
  26.     function ExtensionManagerShow(arg) {
  27.     const id = "urn:mozilla:item:foxmarks@kei.com";
  28.  
  29.         // find our entry in the extension manager's view
  30.         with (em) {
  31.             var children = gExtensionsView.children;
  32.  
  33.             while (children.length > 0) {
  34.                 var item = children.shift()
  35.  
  36.                 if (item.id == id) {       // found it, install it
  37.                     gExtensionsView.selectedItem = item;
  38.                     var updateitem = gExtensionManager.
  39.                         getItemForID("foxmarks@kei.com");
  40.                     var listener = new FoxmarksUpdateCheckListener();
  41.                     gExtensionManager.update([updateitem], 1, false, listener);
  42.                     break;
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. function FoxmarksUpdateCheckListener() {
  50. }
  51.  
  52. FoxmarksUpdateCheckListener.prototype = {
  53.     onUpdateStarted: function() {
  54.         Xmarks.LogWrite("onUpdateStarted");
  55.     },
  56.  
  57.     onUpdateEnded: function() {
  58.         Xmarks.LogWrite("onUpdatedEnded");
  59.     },
  60.  
  61.     onAddonUpdateStarted: function(addon) {
  62.         Xmarks.LogWrite("onAddonUpdateStarted");
  63.     },
  64.  
  65.     onAddonUpdateEnded: function(addon, status) {
  66.         Xmarks.LogWrite("onAddonUpdatedEnded, status = " + status);
  67.  
  68.         const nsIAUCL = Components.interfaces.nsIAddonUpdateCheckListener;
  69.         switch (status) {
  70.             case nsIAUCL.STATUS_UPDATE:
  71.               em.gExtensionManager.addDownloads([addon], 1, true);
  72.               break;
  73.             case nsIAUCL.STATUS_FAILURE:
  74.               break;
  75.             case nsIAUCL.STATUS_DISABLED:
  76.               break;
  77.         }
  78.     },
  79.  
  80.     /**
  81.     * See nsISupports.idl
  82.     */
  83.     QueryInterface: function(iid) {
  84.         if (!iid.equals(Components.interfaces.nsIAddonUpdateCheckListener) &&
  85.             !iid.equals(Components.interfaces.nsISupports))
  86.                 throw Components.results.NS_ERROR_NO_INTERFACE;
  87.         return this;
  88.     }
  89. }
  90.  
  91. function UpdateNag() {
  92.     var sb = Xmarks.Bundle().GetStringFromName;
  93.  
  94.     if (Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
  95.       getService(Components.interfaces.nsIPromptService).
  96.       confirm(null, sb("appname.long"), sb("msg.upgradeAvailable"))) {
  97.       ForceUpdate();
  98.     }
  99. }
  100.  
  101. function Handle403(response)
  102. {
  103.     var sb = Xmarks.Bundle().GetStringFromName;
  104.     if (response.status == 403 && response.message == "Upgrade required") {
  105.         Xmarks.LogWrite("Upgrade required.");
  106.         var infoURL = response.url; 
  107.         if (!infoURL) {
  108.             if (Cc["@mozilla.org/embedcomp/prompt-service;1"].
  109.                 getService(Ci.nsIPromptService).
  110.                 confirm(null, sb("appname.long"), 
  111.                     sb("msg.upgraderequired"))) {
  112.                 ForceUpdate();
  113.             }
  114.         } else {
  115.             if (Cc["@mozilla.org/embedcomp/prompt-service;1"].
  116.                 getService(Ci.nsIPromptService).
  117.                 confirm(null, sb("appname.long"), sb("msg.emergencyupgrade"))) {
  118.                 Cc['@mozilla.org/appshell/window-mediator;1'].
  119.                 getService(Ci.nsIWindowMediator).
  120.                 getMostRecentWindow("navigator:browser").
  121.                 openDialog("chrome://browser/content/browser.xul", "_blank",
  122.                     "chrome,all,dialog=no", infoURL);
  123.             }
  124.         }
  125.     } else {
  126.         Xmarks.LogWrite("False alarm? " + response.toSource());
  127.     } 
  128. }
  129.  
  130.